<?php

namespace App\Console\Commands\Fee;

use App\Console\Commands\Traits\ExcelCommandTrait;
use App\Enums\Fee\FeeEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Library\Rms\RmsShopApi;
use App\Models\Fee\FeeAnnualOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Remote\Rms\RmsShopRemote;
use App\Service\Fee\Import\ImportPaidAnnualFeeOrder;
use App\Service\OpenShop\UserContractService;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Collection;


class ImportWaitPaidFeeOrder extends Command
{

    use ExcelCommandTrait;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fee:import-wait-paid-fee-order';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '导入待支付订单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $arr = [
            [111, '2023'],
            [93, '2023'],
            [99, '2023'],
            [97, '2023'],
            [30, '2023'],
            [37, '2023'],
            [69, '2023'],
            [131, '2023'],
            [167, '2023'],
            [63, '2023'],
            [116, '2023'],
            [129, '2023'],
            [192, '2023'],
            [139, '2023'],
            [1608, '2022'],
            [353, '2023'],
            [1593, '2022'],
            [1630, '2022'],
            [1568, '2022'],
            [1536, '2022'],
            [1556, '2023'],
            [1178, '2023'],
            [1377, '2023'],
            [398, '2023'],
            [333, '2023'],
            [382, '2023'],
            [362, '2023'],
            [512, '2023'],
            [380, '2023'],
            [600, '2023'],
            [272, '2023'],
            [523, '2023'],
            [291, '2023'],
            [229, '2023'],
            [357, '2023'],
            [1869, '2023'],
            [1379, '2022'],
            [369, '2023'],
            [2199, '2022'],
            [3199, '2022'],
            [3069, '2022'],
            [1092, '2023'],
            [3208, '2022'],
        ];

        $data = [];

        foreach ($arr as $v) {
            $shopCode = $v[0];
            $year     = $v[1];

            $signOrder = OpenShopSignOrderModel::query()
                                               ->where('shopCode', $shopCode)
                                               ->where('status', '!=', SignOrderEnum::ORDER_STATUS_NO)
                                               ->orderByDesc('id')
                                               ->first();
            if (!$signOrder) {
                dd($shopCode . '-不存在signOrder');
            }

            $shopId = $signOrder->progressRel->shopId;
            if (!$shopId) {
                dd($shopCode, '-不存在shopId');
            }

            $startFeeTime = $signOrder->progressRel->startFeeTime;
            if (!$startFeeTime) {
                dd($shopCode . '-不存在的startFeeTime');
            }


            $feeTime = strtotime($year . substr($startFeeTime, 4, 4));
            $endTime = strtotime(date('Y-m-d', $feeTime) . ' 23:59:59');
            if ($endTime < time()) {
                $endTime = strtotime(date('Y-m-d 23:59:59')) + 86400 * 10;
            }

            $checkOrder = FeeAnnualOrderModel::query()->where('shopId', $shopId)->where('endTime', $endTime)->first();
            if ($checkOrder) {
                $this->info($shopCode . '已存在...');
                continue;
            }

            $kfCash = RmsShopRemote::functionOpenDetail($shopId)['functionOpenDetail'][4]['isOpen'] ?? false;

            $managePrice = $signOrder->progressRel->managePrice;

            $contractManage = $managePrice ?: UserContractService::getManagementCostEveryYear($signOrder->orderNo);

            $data[] = [
                'feeOrderNo'     => CreateAnnualFeeOrder::generateFeeOrderNo(),
                'shopId'         => $shopId,
                'shouldPayPrice' => $kfCash ? ($contractManage + FeeEnum::FEE_SYSTEM) : $contractManage,
                'payStatus'      => FeeAnnualOrderModel::PAY_STATUS_WAIT,
                'manageFee'      => $contractManage,
                'systemFee'      => $kfCash ? FeeEnum::FEE_SYSTEM : 0,
                'endTime'        => $endTime,
                'createdAt'      => time(),
                'updatedAt'      => time(),
                'startYear'      => (int)$year,
//                'endDate' => date('Y-m-d H:i:s', $endTime),
            ];
        }

        FeeAnnualOrderModel::query()->insert($data);

        dump($data);
    }
}
